package com.spring.helper;

import com.spring.annotation.Controller;
import org.apache.commons.collections4.CollectionUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 将带有@Controller注解的类以及其实例添加到beanMap中
 */
public class BeanHelper {

    public static Map<Class<?>, Object> BeanMap;//bean容器

    static {
        BeanMap = new HashMap<Class<?>, Object>();
        Set<Class<?>> classSet = ClassHelper.CLASS_SET;
        if (!CollectionUtils.isEmpty(classSet)) {
            for (Class<?> cls : classSet) {
                if (cls.isAnnotationPresent(Controller.class)) {
                    try {
                        Object obj = cls.newInstance();
                        BeanMap.put(cls, obj);
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }

            }
        }

    }
}
